package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 通过这个类来完成对  blog 表的操作
public class BlogDao {
    // 1.  新增操作 (提交博客)
    public void insert(Blog blog) {
        // 1. 先将需要用到的资源创建出来
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 2. 通过 DBUtil 获取连接
            connection = DBUtil.getConnection();
            // 3. 编写 SQL
            String sql = "insert into blog values(null, ?, ?, now(), ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            // 4. 执行 SQL
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }

    // 2. 查询博客列表 (博客列表页)
    public List<Blog> getBlogs(int userId) {
        // 先把需要使用到的资源 创建处理 (设为null即可)
        Connection connection = null;
        PreparedStatement statement =null;
        ResultSet resultSet = null;
        try {
            // 1. 通过 DBUtil 获取连接
            connection = DBUtil.getConnection();
            // 2. 编写 SQL
            String sql = "select * from blog where user_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            // 3. 执行 SQL 获取结果集
            resultSet = statement.executeQuery();
            // 4. 定义一个 blogList 集合 用来最后返回
            List<Blog> blogList = new ArrayList<>();
            // 5. 遍历结果集 构造 blog 对象 添加到集合中
            while (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blog_id"));
                blog.setTitle(resultSet.getString("title"));
                // 这里不是要显示正文的所有内容，而是显示一个摘要
                // 所以这里 先获取到整个正文 然后用 subString() 截断
                String content = resultSet.getString("content");
                if(content.length() >= 20) {
                    content = content.substring(1, 20);
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("post_time"));
                blog.setUserId(resultSet.getInt("user_id"));
                blogList.add(blog);
            }
            return blogList;
         } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
    }

    // 3. 根据 博客Id 查询对应的博客
    public Blog getBlog(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blog_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                Blog blog = new Blog();
                blog.setUserId(resultSet.getInt("user_id"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("post_time"));
                blog.setUserId(resultSet.getInt("user_id"));
                return blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    // 4. 通过 博客Id 删除对应用户的博客
    public void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from blog where blog_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection , statement, null);
        }
    }

    // 5. 通过用户id 查询用户的博客总数
    public int count(Integer userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select count(*) from blog where user_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            resultSet = statement.executeQuery();
            return resultSet.getInt(1);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
    }

}
